<?php
require_once './modules/getid3/getid3.php';

class Browse {
	var $dirName;
	
	public function setDir($thisDirName) {
		$this->dirName = htmlentities($thisDirName);
	}
	
	public function getDir($strPath) {
		$getter = new Getter();
		
		if(is_dir($strPath)) {
			/*
			Table browse header
			*/
			echo '<table cellspacing="0" cellpadding="0" border="0" class="browse">' . CR_LF;
			echo '<tr>' . CR_LF;
			if(strpos($getter->get("action"), "bulk")) {
				echo '	<th class="action_select">select</th>' . CR_LF;
			}
			echo '	<th>type</th>' . CR_LF;
			echo '	<th>filename</th>' . CR_LF;
			echo '	<th>length</th>' . CR_LF;
			echo '	<th>filesize</th>' . CR_LF;
			echo '	<th>bitrate</th>' . CR_LF;
			echo '	<th>rename</th>' . CR_LF;
			echo '	<th>delete</th>' . CR_LF;
			echo '</tr>' . CR_LF;
			
			$getid3 = new getID3;
			$getid3->encoding = 'UTF-8';
			
			if($dh = opendir($strPath)) {
				while(($file = readdir($dh)) !== false) {
					if(!preg_match("/^\./", $file)) {
						
						echo '<tr>' . CR_LF;
						if(strpos($getter->get("action"), "bulk")) {
							echo '	<td class="action_select" align="center">' . CR_LF;
							
							if($getter->get("option") == "select_all") {
								echo '		<input type="checkbox" checked="checked" name="item" value="' . $file . '" />' . CR_LF;
							}
							else {
								echo '		<input type="checkbox" name="item" value="' . $file . '" />' . CR_LF;
							}
							
							echo '	</td>' . CR_LF;
						}
						
						echo '	<td class="type">' . filetype($strPath . '/' . $file) . '</td>' . CR_LF;
						if(filetype($strPath . '/' . $file) == "dir") {
							if(OS == "windows") {
								echo '	<td class="filename"><a href="?path=' . urlencode($strPath) . '\\' . urlencode($file) . '">' . htmlentities($file) . '</a></td>' . CR_LF;
							}
							else {
								echo '	<td class="filename"><a href="?path=' . urlencode($strPath) . '/' . urlencode($file) . '">' . htmlentities($file) . '</a></td>' . CR_LF;
							}
						}
						else {
							echo '	<td class="filename">' . htmlentities($file) . '</td>' . CR_LF;
						}
						
						try {
							$getid3->Analyze($strPath . SLASH . $file);
							
							echo '	<td class="action length">' . @$getid3->info['playtime_string'] . '</td>' . CR_LF;
							echo '	<td class="action size">' . number_format(@$getid3->info['filesize']) . '</td>' . CR_LF;
							echo '	<td class="action bitrate">' . number_format(@$getid3->info['audio']['bitrate']) . '</td>' . CR_LF;
						}
						catch(Exception $e) {
							echo '	<td class="action"></td>' . CR_LF;
							echo '	<td class="action"></td>' . CR_LF;
							echo '	<td class="action"></td>' . CR_LF;
						}
						
						echo '	<td class="action"><a href="?path=' . $strPath . '&amp;action=single_rename&amp;item=' . $file . '"><img src="icons/folder_edit.png" border="0" alt="Rename folder" /></a></td>' . CR_LF;
						echo '	<td class="action"><a href="?path=' . $strPath . '&amp;action=single_delete&amp;item=' . $file . '"><img src="icons/folder_delete.png" border="0" alt="Delete folder" /></a></td>' . CR_LF;
						echo '</tr>' . CR_LF;
					
					}
				}
				closedir($dh);
			}
			echo '</table>' . CR_LF;
		}
		else {
			echo '<p class="error">The path ' . $strPath . ' does not exist</p>' . CR_LF;
		}
	}
	
	public function renameSingle($strPath) {
			/*
			Table rename header
			*/
			echo '<form method="post" action="actions.php">' . CR_LF;;
			echo '<table cellspacing="0" cellpadding="0" border="0" class="browse">' . CR_LF;
			echo '<tr>' . CR_LF;
			echo '	<th>filename</th>' . CR_LF;
			echo '</tr>' . CR_LF;
			echo '<tr>' . CR_LF;
			echo '	<td><input type="text" name="new_item" class="rename_item" value="' . $_GET["item"] . '" /></td>' . CR_LF;
			echo '</tr>' . CR_LF;
			echo '</table>' . CR_LF;
			echo '<input type="hidden" name="path" value="' . $strPath . '" />' . CR_LF;
			echo '<input type="hidden" name="old_item" value="' . $_GET["item"] . '" />' . CR_LF;
			echo '<br />' . CR_LF;
			echo '<input type="submit" name="singlerename" value="rename" />' . CR_LF;
			echo '<input type="reset" onclick="javascript: history.back(-1);" name="back" value="back" />' . CR_LF;
			echo '</form>' . CR_LF;
	}
}
?>